const { prompt } = require("inquirer");
const program = require("commander");
const chalk = require("chalk");
const download = require("download-git-repo");
const ora = require("ora");
const fs = require("fs");
const TemplateList = ["antd-design-pro", "Next.js"];
const option = program.parse(process.argv).args[0];
console.log("option:", option);
console.log(111);
const defaultName = typeof option === "string" ? option : "hz-antd-pro-cli";

const question = [
  {
    type: "input",
    name: "name",
    message: "input project name ?",
    default: defaultName,
    filter(val) {
      return val.trim();
    },
    validate(val) {
      const validate = val.trim().split(" ").length === 1;
      return validate || "Project name is not allowed to have spaces";
    },
    transformer(val) {
      return val;
    },
  },
  {
    type: "list",
    name: "template",
    message: "Select project template ?",
    choices: TemplateList,
    default: TemplateList[0],
    validate(val) {
      return true;
    },
    transformer(val) {
      return val;
    },
  },
  {
    type: "input",
    name: "description",
    message: "input project description ?",
    default: "hz antd desing pro project",
    validate(val) {
      return true;
    },
    transformer(val) {
      return val;
    },
  },
  {
    type: "input",
    name: "author",
    message: "Author",
    default: "input project author",
    validate(val) {
      return true;
    },
    transformer(val) {
      return val;
    },
  },
];
module.exports = prompt(question).then(
  ({ name, template, description, author }) => {
    const spinner = ora("Downloading please wait...");
    spinner.start();
    let url = "gitlab:techdoc.oa.com:QixinTeam/qixin-node-merchant-pc";
    download(
      `direct:http://techdoc.oa.com/QixinTeam/qixin-review-pc.git`,
      `./${name}`,
      { clone: true },
      (err) => {
        if (err) {
          console.log(chalk.red(err));
          process.exit();
        }
        fs.readFile(`./${name}/package.json`, "utf8", (err, data) => {
          if (err) {
            spinner.stop();
            console.error(err);
            return;
          }
          const packageJson = JSON.parse(data);
          packageJson.name = name;
          packageJson.description = description;
          packageJson.author = author;
          fs.writeFile(
            `./${name}/package.json`,
            JSON.stringify(packageJson, null, 2),
            "utf8",
            (err) => {
              if (err) {
                spinner.stop();
                console.error(err);
                return;
              }
              spinner.stop();
              console.log(chalk.green("project init successfully!"));
              console.log(`
          ${chalk.bgWhite.black("   Run Application  ")}
          ${chalk.yellow(`cd ${name}`)}
          ${chalk.yellow("npm install")}
          ${chalk.yellow("npm start")}
        `);
            }
          );
        });
      }
    );
  }
);
